package com.rop.znyx.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.activity.service.SecKillService;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.activity.SecKill;
import com.rop.znyx.vo.activity.SecKillQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/7/11
 * Time: 9:28
 * Description: 秒杀管理模块
 */

@Api(tags = "秒杀活动管理")
@RestController
@RequestMapping(value = "/admin/activity/SecKill")
@RequiredArgsConstructor

public class SecKillController {

    private final SecKillService SecKillService;

    /**
     * 获取分页列表
     */
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<SecKill>> getList(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "SecKillQueryVo", value = "查询对象")
            SecKillQueryVo SecKillQueryVo) {

        Page<SecKill> pageParam = new Page<>(page, limit);
        IPage<SecKill> pageModel = SecKillService.selectPage(pageParam, SecKillQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 根据id获取秒杀活动
     */
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result<SecKill> get(@PathVariable Long id) {
        SecKill SecKill = SecKillService.getById(id);
        return Result.ok(SecKill);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody SecKill SecKill) {
        boolean isSuccess = SecKillService.save(SecKill);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result<Boolean> updateById(@RequestBody SecKill SecKill) {
        boolean isSuccess = SecKillService.updateById(SecKill);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        boolean isSuccess = SecKillService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据id列表删除
     */
    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = SecKillService.removeByIds(idList);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 更新状态
     */
    @ApiOperation(value = "更新状态")
    @PostMapping("updateStatus/{id}/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean isSuccess = SecKillService.updateStatus(id, status);
        return isSuccess ? Result.ok() : Result.fail();
    }
}
